//package com.gtp.mq.demo.expands;
//
//import java.util.Iterator;
//import java.util.Map;
//
//import org.aopalliance.aop.Advice;
//import org.aopalliance.intercept.MethodInterceptor;
//import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder;
//import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder.StatelessRetryInterceptorBuilder;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer;
//import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
//import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
//import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
//import org.springframework.beans.factory.support.DefaultListableBeanFactory;
//
///**
// * 容器后置处理器,修改指定类的一些属性
// * 给消费者增加AdviceChain
// */
//public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
//
//	@Override
//	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//		
//		//前置条件判断
//		if(!(beanFactory instanceof DefaultListableBeanFactory)){
//			return;
//		}
//		
//		//根据类型获取bean,然后动态设置
//		DefaultListableBeanFactory acf = (DefaultListableBeanFactory) beanFactory;
//		
//		//发消息:basicPublish
//		Map<String, RabbitTemplate> rt = acf.getBeansOfType(RabbitTemplate.class);
//		if (rt != null && !rt.isEmpty()) {
//			Iterator<String> it = rt.keySet().iterator();
//			while(it.hasNext()){
//				String k = it.next();
//				RabbitTemplate v = rt.get(k);
//				//发消息前:basicPublish,注意是addBeforePublishPostProcessors,防止替换默认或其它
//				//v.setBeforePublishPostProcessors(new MyMessagePostProcessor());
//				v.addBeforePublishPostProcessors(new MyMessagePostProcessor());
//				//basicGet前调用
//				//v.setAfterReceivePostProcessors(afterReceivePostProcessors);
//			}
//		}
//		
//		//收消息
//		Map<String, AbstractMessageListenerContainer> ms = acf.getBeansOfType(AbstractMessageListenerContainer.class);
//
//		if (ms != null && !ms.isEmpty()) {
//			Iterator<String> it = ms.keySet().iterator();
//			while (it.hasNext()) {
//				String key = it.next();
//				SimpleMessageListenerContainer slc = (SimpleMessageListenerContainer)ms.get(key);
//				try{
//					//issue fix:但是这个不能修改之前的,只能替换,如何巧妙设置?
//					Advice[] args={retryAdvice()};
//					slc.setAdviceChain(args);
//				}catch(Exception e){
//					//
//				}
//			}
//		}
//	}
//	
//	//重试机制也是advice
//    private MethodInterceptor retryAdvice(){
//    	
//    	StatelessRetryInterceptorBuilder builder = RetryInterceptorBuilder.stateless();
//    	builder.maxAttempts(3);
//    	builder.backOffOptions(3000,1, 10000);
//    	builder.recoverer(new RejectAndDontRequeueRecoverer());//必须
//    	
//		return builder.build();
//	}
//    
//    /*
//		RejectAndDontRequeueRecoverer  打日志，丢弃
//		RepublishMessageRecoverer      重新入队列
//		建议打日志丢弃,因为丢到死信队列中，还得有人处理，不然堆死了!
//     */
//
//	
//}